/**
 * 
 */
package com.cater.talents.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.School;
import com.cater.sys.service.impl.SchoolServiceImpl;
import com.cater.talents.entity.EduExperience;
import com.cater.talents.entity.WorkExperience;
import com.cater.talents.service.EduExperienceServiceImpl;

/**
 * @Description: TODO
 * @author: yong
 * @time:2018年4月23日 上午12:40:26
 */
@Controller
@RequestMapping("/edu")
public class EduExperienceController {
	@Autowired
	private EduExperienceServiceImpl service;
	@Autowired
	private SchoolServiceImpl schoolService;// 学校

	@RequestMapping(value = "addView")
	public String addView(Model model, String talents_id, CommonQuery query, Integer num) {
		model.addAttribute("talents_id", talents_id);
		String url = "";
		if (num == 1) {
			url = "talents/eduExperience/addschool";
		}
		if (num == 2) {
			url = "talents/eduExperience/addtrain";
		}
		return url;
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "addshool")
	@ResponseBody
	@SystemControllerLog(description = "添加教育经历")
	public JSONObject add(Integer num, CommonQuery query, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		try {
			for (int i = 1; i < num + 1; i++) {
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				String schoolName = request.getParameter("schoolName" + i);
				String profession = request.getParameter("profession" + i);
				String education = request.getParameter("education" + i);
				String unified = request.getParameter("unified" + i);
				EduExperience experience = new EduExperience();
				if (StringUtils.isNotEmpty(schoolName)) {
					School edu = new School();
					query.setName(schoolName);
					edu = schoolService.getByName(query);
					if (edu != null) {
						edu.setId(edu.getId());
					} else {
						edu = new School();
						edu.setName(schoolName);
						edu.setClassify("1");
						String edu_id = schoolService.saves(edu);
						edu.setId(edu_id);
					}
					experience.setSchool(edu);
					experience.setSchoolName(schoolName);
					experience.setType("1");
					experience.setTalentsId(talentid);
					experience.setStartDate(startDate);
					if (StringUtils.isNotEmpty(endDate)) {
						experience.setEndDate(endDate);
					} else {
						experience.setEndDate("至今");
					}
					experience.setProfession(profession);
					experience.setEducation(education);
					experience.setUnified(Integer.valueOf(unified));
					service.saves(experience);
				}
			}
			json.put("code", 200);
			json.put("message", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "操作失败");
		}

		return json;
	}

	@RequestMapping(value = "addtrain")
	@ResponseBody
	@SystemControllerLog(description = "添加培训经历")
	public JSONObject addTrain(Integer num, CommonQuery query, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		try {
			for (int i = 1; i < num + 1; i++) {
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				// String schoolId = request.getParameter("schoolId" + i);
				String schoolName = request.getParameter("schoolName" + i);
				String course = request.getParameter("course" + i);
				String grade = request.getParameter("grade" + i);
				EduExperience experience = new EduExperience();
				if (StringUtils.isNotEmpty(schoolName)) {
					School edu = new School();
					query.setName(schoolName);
					edu = schoolService.getByName(query);
					if (edu != null) {
						edu.setId(edu.getId());
					} else {
						edu = new School();
						edu.setName(schoolName);
						edu.setClassify("2");
						String edu_id = schoolService.saves(edu);
						edu.setId(edu_id);
					}
					experience.setSchool(edu);
					experience.setSchoolName(schoolName);
					experience.setType("2");
					experience.setTalentsId(talentid);
					experience.setStartDate(startDate);
					experience.setEndDate(endDate);
					experience.setCourse(course);
					experience.setGrade(grade);
					service.saves(experience);
					json.put("code", 200);
					json.put("message", "添加成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "操作失败");
		}
		return json;
	}

	@RequestMapping(value = "editView")
	public String editView(Model model, CommonQuery query, Integer num) {
		model.addAttribute("talentsId", query.getTalentsId());
		String url = "";
		if (num == 1) {
			query.setType("1");
			List<EduExperience> schools = service.getListByTalentsId(query);
			model.addAttribute("schoolnum", schools.size());
			model.addAttribute("schooledus", schools);
			url = "talents/eduExperience/update_school";
		}
		if (num == 2) {
			query.setType("2");
			List<EduExperience> trains = service.getListByTalentsId(query);
			model.addAttribute("trainnum", trains.size());
			model.addAttribute("trainedus", trains);
			url = "talents/eduExperience/update_train";
		}
		return url;
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "updateshool")
	@ResponseBody
	@SystemControllerLog(description = "修改教育经历")
	public JSONObject update(Integer num, HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		query.setType("1");
		List<EduExperience> eduExperiences = service.getListByTalentsId(query);
		try {
			for (int i = 1; i < num + 1; i++) {
				String id = request.getParameter("id" + i);
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				// String schoolId = request.getParameter("schoolId" + i);
				String temp_schoolName = request.getParameter("schoolName" + i);
				String schoolName = null;
				if (StringUtils.isNotEmpty(temp_schoolName)) {
					schoolName = temp_schoolName.replaceAll("'", "");
				}
				String profession = request.getParameter("profession" + i);
				String education = request.getParameter("education" + i);
				String unified = request.getParameter("unified" + i);
				for (EduExperience eduExperience : eduExperiences) {
					if (eduExperience.getId().equals(id)) {
						if (StringUtils.isNotEmpty(schoolName)) {
							School edu = new School();
							query.setName(schoolName);
							edu = schoolService.getByName(query);
							if (edu != null) {
								edu.setId(edu.getId());
							} else {
								edu = new School();
								edu.setName(schoolName);
								edu.setClassify("1");
								String edu_id = schoolService.saves(edu);
								edu.setId(edu_id);
							}
							eduExperience.setSchool(edu);
							eduExperience.setSchoolName(schoolName);
							eduExperience.setStartDate(startDate);
							eduExperience.setTalentsId(talentid);
							if (StringUtils.isNotEmpty(endDate)) {
								eduExperience.setEndDate(endDate);
							} else {
								eduExperience.setEndDate("至今");
							}
							eduExperience.setProfession(profession);
							eduExperience.setEducation(education);
							eduExperience.setUnified(Integer.valueOf(unified));
							service.update(eduExperience);
						}
					}
				}
				if (StringUtils.isEmpty(id)) {
					EduExperience experience = new EduExperience();
					if (StringUtils.isNotEmpty(schoolName)) {
						School edu = new School();
						query.setName(schoolName);
						edu = schoolService.getByName(query);
						if (edu != null) {
							edu.setId(edu.getId());
						} else {
							edu = new School();
							edu.setName(schoolName);
							edu.setClassify("1");
							String edu_id = schoolService.saves(edu);
							edu.setId(edu_id);
						}
						experience.setSchool(edu);
						experience.setSchoolName(schoolName);
						experience.setType("1");
						experience.setTalentsId(talentid);
						experience.setStartDate(startDate);
						if (StringUtils.isNotEmpty(endDate)) {
							experience.setEndDate(endDate);
						} else {
							experience.setEndDate("至今");
						}
						experience.setProfession(profession);
						experience.setEducation(education);
						experience.setUnified(Integer.valueOf(unified));
						service.saves(experience);
					}
				}
			}
			json.put("code", 200);
			json.put("message", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "操作失败");
		}
		return json;
	}

	@RequestMapping(value = "updatetrain")
	@ResponseBody
	@SystemControllerLog(description = "修改培训经历")
	public JSONObject updateTrain(Integer num, HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		query.setType("1");
		List<EduExperience> eduExperiences = service.getListByTalentsId(query);
		try {
			for (int i = 1; i < num + 1; i++) {
				String id = request.getParameter("id" + i);
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				// String schoolId = request.getParameter("schoolId" + i);
				String temp_schoolName = request.getParameter("schoolName" + i);
				String schoolName = null;
				if (StringUtils.isNotEmpty(temp_schoolName)) {
					schoolName = temp_schoolName.replaceAll("'", "");
				}
				String course = request.getParameter("course" + i);
				String grade = request.getParameter("grade" + i);
				for (EduExperience eduExperience : eduExperiences) {
					if (eduExperience.getId().equals(id)) {
						if (StringUtils.isNotEmpty(schoolName)) {
							School edu = new School();
							query.setName(schoolName);
							edu = schoolService.getByName(query);
							if (edu != null) {
								edu.setId(edu.getId());
							} else {
								edu = new School();
								edu.setName(schoolName);
								edu.setClassify("1");
								String edu_id = schoolService.saves(edu);
								edu.setId(edu_id);
							}
							eduExperience.setSchool(edu);
							eduExperience.setSchoolName(schoolName);
							eduExperience.setTalentsId(talentid);
							eduExperience.setStartDate(startDate);
							eduExperience.setEndDate(endDate);
							eduExperience.setCourse(course);
							eduExperience.setGrade(grade);
							service.update(eduExperience);
						}
					}
				}
				if (StringUtils.isEmpty(id)) {
					EduExperience experience = new EduExperience();
					if (StringUtils.isNotEmpty(schoolName)) {
						School edu = new School();
						query.setName(schoolName);
						edu = schoolService.getByName(query);
						if (edu != null) {
							edu.setId(edu.getId());
						} else {
							edu = new School();
							edu.setName(schoolName);
							edu.setClassify("2");
							String edu_id = schoolService.saves(edu);
							edu.setId(edu_id);
						}
						experience.setSchool(edu);
						experience.setSchoolName(schoolName);
						experience.setType("2");
						experience.setTalentsId(talentid);
						experience.setStartDate(startDate);
						experience.setEndDate(endDate);
						experience.setCourse(course);
						experience.setGrade(grade);
						service.saves(experience);
					}
				}
			}
			json.put("code", 200);
			json.put("message", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "操作失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除教育培训经历")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping(value = "editSingleView")
	public String editSingleView(Integer num, String id, Model model) {
		EduExperience eduExperience = service.getById(id);
		String url = "";
		if (num == 1) {
			model.addAttribute("eduExp", eduExperience);
			url = "talents/eduExperience/edit_single";
		} else if (num == 2) {
			model.addAttribute("trainExp", eduExperience);
			url = "talents/eduExperience/edit_train_single";
		}
		return url;

	}

	@RequestMapping(value = "editSingle")
	@ResponseBody
	@SystemControllerLog(description = "修改教育经历")
	public JSONObject updateSingle(Integer num, EduExperience eduExperience, HttpServletRequest request,
			CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String schoolName = eduExperience.getSchoolName();
			if (StringUtils.isNotEmpty(schoolName)) {
				if (num == 1) {
					School edu = new School();
					query.setName(schoolName);
					edu = schoolService.getByName(query);
					if (edu != null) {
						edu.setId(edu.getId());
					} else {
						edu = new School();
						edu.setName(schoolName);
						edu.setClassify("1");
						String edu_id = schoolService.saves(edu);
						edu.setId(edu_id);
					}
					eduExperience.setSchool(edu);
					service.update(eduExperience);
					json.put("code", 200);
					json.put("message", "修改成功");
				} else if (num == 2) {
					School edu = new School();
					query.setName(schoolName);
					edu = schoolService.getByName(query);
					if (edu != null) {
						edu.setId(edu.getId());
					} else {
						edu = new School();
						edu.setName(schoolName);
						edu.setClassify("2");
						String edu_id = schoolService.saves(edu);
						edu.setId(edu_id);
					}
					eduExperience.setSchool(edu);

				}
				service.update(eduExperience);
				json.put("code", 200);
				json.put("message", "修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "修改失败");
		}
		return json;

	}
	

	@ResponseBody
	@RequestMapping("/getJsonBy")
	public JSONObject getJsonBy(HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			List<EduExperience> list = service.getListBy(query);
			if (list != null && list.size() > 0) {
				json.put("datas", list);
				json.put("code", 200);
				json.put("msg", "数据获取成功");
			} else {
				json.put("code", 400);
				json.put("msg", "暂无数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("msg", "数据获取异常");
		}
		return json;
	}
}
